package com.yanqu.road.server.manager.rank;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.cross.CrossGroupMonthRankDaoImpl;
import com.yanqu.road.dao.impl.cross.CrossMonthRankUploadOverDaoImpl;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.rank.cross.CrossMonthRankUploadOver;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.rank.cross.RankGradeInfo;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossGroupMonthRankListModel;
import com.yanqu.road.logic.cross.CrossGroupNumMonthRankListModel;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.user.CrossUserNotifyMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.util.ConcurrentHashSet;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossGroupUserMonthAbilityRankMgr extends TempMgr {

    private static final Logger logger = LogManager.getLogger(CrossGroupUserMonthAbilityRankMgr.class.getName());

    //groupId，month
    private static Map<Integer, Map<Integer, CrossGroupMonthRankListModel>> groupRankListModelMap = new ConcurrentHashMap<>();
    //groupServerNum month
    private static Map<Integer, Map<Integer, CrossGroupNumMonthRankListModel>> groupNumRankListModelMap = new ConcurrentHashMap<>();
    //month serverIds
    private static Map<Integer, CrossMonthRankUploadOver> uploadOverServerIdsMap = new ConcurrentHashMap<>();
    //groupId month
    private static Map<Integer, Map<Integer, Boolean>> groupUploadOverMap = new ConcurrentHashMap<>();

    //groupId serverList
    private static Map<Integer, Set<Long>> beforeChangeGroupServerList = new ConcurrentHashMap<>();

    //serverNum groupId userId
    private static Map<Integer, Map<Integer, Map<Long, CrossUserRank>>> groupUserDataCatch = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        CrossUserNotifyMgr.addSyncListener(new MonthRankUserBaseSyncListener());
        addServerGroupReloadListener();
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        initUploadOverServerIds();
        initGroupRankListModel();
        initGroupUploadOver();
        initBeforeChangeGroupServerList();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (Map<Integer, CrossGroupMonthRankListModel> rankListModelMap : groupRankListModelMap.values()) {
            for (CrossGroupMonthRankListModel rankListModel : rankListModelMap.values()) {
                rankListModel.saveRankList();
            }
        }

        for (Map<Integer, CrossGroupNumMonthRankListModel> rankListModelMap : groupNumRankListModelMap.values()) {
            for (CrossGroupNumMonthRankListModel rankListModel : rankListModelMap.values()) {
                rankListModel.saveRankList();
            }
        }

        for (CrossMonthRankUploadOver serverUploadOver : uploadOverServerIdsMap.values()) {
            if (serverUploadOver.isInsertOption()) {
                new CrossMonthRankUploadOverDaoImpl().add(serverUploadOver);
            } else if (serverUploadOver.isUpdateOption()) {
                new CrossMonthRankUploadOverDaoImpl().update(serverUploadOver);
            }
        }

        removeOutTimeData();

        return true;
    }

    public static void initGroupRankListModel() {
        Map<Integer, Map<Integer, LinkedList<CrossUserRank>>> tempDataMap = new CrossGroupMonthRankDaoImpl().getCrossGroupMonthRankListMap(DateHelper.getMonth());
        for (Map.Entry<Integer, Map<Integer, LinkedList<CrossUserRank>>> mapEntry : tempDataMap.entrySet()) {
            int groupId = mapEntry.getKey();
            Map<Integer, LinkedList<CrossUserRank>> monthMap = mapEntry.getValue();
            for (Map.Entry<Integer, LinkedList<CrossUserRank>> listEntry : monthMap.entrySet()) {
                int month = listEntry.getKey();
                CrossGroupMonthRankListModel listModel = getCrossGroupRankListModelMap(groupId, month, true);
                if (null == listModel){
                    continue;
                }
                listModel.setRankNum(listEntry.getValue().size());
                CrossMonthRankUploadOver uploadData = getMonthUploadData(month);
                if (null != uploadData) {
                    listModel.setRankNum(uploadData.getRankNum(groupId));
                }
                listModel.setRankList(listEntry.getValue());
            }
        }

        tempDataMap = new CrossGroupMonthRankDaoImpl().getCrossGroupNumMonthRankListMap(DateHelper.getMonth());
        for (Map.Entry<Integer, Map<Integer, LinkedList<CrossUserRank>>> mapEntry : tempDataMap.entrySet()) {
            int serverNum = mapEntry.getKey();
            Map<Integer, LinkedList<CrossUserRank>> monthMap = mapEntry.getValue();
            for (Map.Entry<Integer, LinkedList<CrossUserRank>> listEntry : monthMap.entrySet()) {
                int month = listEntry.getKey();
                CrossGroupNumMonthRankListModel listModel = getCrossGroupNumRankListModelMap(serverNum, month, true);
                if (null == listModel){
                    continue;
                }
                listModel.setRankNum(listEntry.getValue().size());
                listModel.setRankList(listEntry.getValue());
            }
        }
    }

    public static void initUploadOverServerIds() {
        uploadOverServerIdsMap.clear();
        uploadOverServerIdsMap = new CrossMonthRankUploadOverDaoImpl().getUploadOverServerIds(DateHelper.getMonth());
    }

    public static void initGroupUploadOver() {
        groupUploadOverMap.clear();
        Map<Integer, List<CrossUnionServerGroup>> serverListMap = CrossUnionServerGroupMgr.getServerListMapByServerNum(ConfigHelper.getInt("channelId"));
        Set<Integer> serverNumSet = new HashSet<>();
        for (Map.Entry<Integer, List<CrossUnionServerGroup>> listEntry : serverListMap.entrySet()) {
            int serverNum = listEntry.getKey();
            if (serverNum < GameConfig.UNION_PLAYER_OPEN_COND_AREA_NUM) {
                continue;
            }
            if(!checkGroupUploadOver(serverNum, listEntry.getValue(), true)){
                serverNumSet.add(serverNum);
            }
        }

        removeUploadServerIdByServerNum(serverNumSet);

    }

    private static CrossMonthRankUploadOver getMonthUploadData(int month) {
        synchronized (uploadOverServerIdsMap) {
            return uploadOverServerIdsMap.get(month);
        }
    }

    public static void checkGroupUploadOverTask() {
        if (groupUserDataCatch.isEmpty()) {
            return;
        }

        Map<Integer, List<CrossUnionServerGroup>> serverListMap = CrossUnionServerGroupMgr.getServerListMapByServerNum(ConfigHelper.getInt("channelId"));
        for (Map.Entry<Integer, List<CrossUnionServerGroup>> listEntry : serverListMap.entrySet()) {
            int serverNum = listEntry.getKey();
            if (serverNum < GameConfig.UNION_PLAYER_OPEN_COND_AREA_NUM) {
                continue;
            }
            if (!groupUserDataCatch.containsKey(serverNum)) {
                continue;
            }

            checkGroupUploadOver(serverNum, listEntry.getValue(), false);
        }
    }

    public static boolean checkGroupUploadOver(int serverNum, List<CrossUnionServerGroup> serverGroups, boolean isLoad) {
        int month = DateHelper.getMonth();
        Set<Long> uploadOverserverIds = null;
        Set<Long> needServerIds = new HashSet<>();
        for (CrossUnionServerGroup serverGroup : serverGroups) {
            needServerIds.addAll(serverGroup.getServerList());
        }

        CrossMonthRankUploadOver monthRankUpload = uploadOverServerIdsMap.get(month);
        if (null != monthRankUpload) {
            uploadOverserverIds = new ConcurrentHashSet<>(monthRankUpload.getServerIds());
        }
        boolean isOver = true;
        if (null != uploadOverserverIds) {
            for (Long serverId : needServerIds) {
                if (!uploadOverserverIds.contains(serverId)) {
                    isOver = false;
                    break;
                }
            }
        } else {
            isOver = false;
        }

        if (isOver && !isLoad) {
            Map<Integer, Map<Long, CrossUserRank>> rankMap = groupUserDataCatch.get(serverNum);
            if (null != rankMap) {
                //先设置同区服数的总榜的
                CrossGroupNumMonthRankListModel serverNumRankModel = getCrossGroupNumRankListModelMap(serverNum, month, true);
                int totalRankNum = 0;
                for (Map<Long, CrossUserRank> userRankMap : rankMap.values()) {
                    int maxTakeNum = GameConfig.UNION_PLAYER_MONTH_ABILITY_RANK_NUM_MAX;
                    List<CrossUserRank> userRankList = new ArrayList<>();
                    userRankList.addAll(userRankMap.values());
                    userRankList.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
                    //每组最多取500个
                    for (int i = 0; i < userRankList.size() && i < maxTakeNum; i++) {
                        serverNumRankModel.addUserRank(userRankList.get(i));
                        totalRankNum++;
                    }
                }
                //排序并设置排行长度
                serverNumRankModel.setRankNum(totalRankNum);
                serverNumRankModel.endSort();

                getLogger().info("区服数{}的所有分组共{}活跃玩家", serverNum, serverNumRankModel.getNowRankNum());

                List<RankGradeInfo> rankGradeInfoList = ConfigMgr.getMonthAbilityRankGradeConfigs();
                Map<Integer, BigInteger> minValueMap = serverNumRankModel.getGradeMinValueMap(rankGradeInfoList);

                //再设置组内榜单
                for (Map.Entry<Integer, Map<Long, CrossUserRank>> mapEntry : rankMap.entrySet()) {
                    int groupId = mapEntry.getKey();
                    int rankNum = mapEntry.getValue().size();
                    long rankTime = monthRankUpload.getRankTime(groupId);
                    CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroup(groupId);
                    if (null == serverGroup){
                        continue;
                    }
                    if (0 < rankTime && serverGroup.getChangTime() < rankTime) {
                        //分组变化时间小于评定时间,不评定
                        continue;
                    }
                    CrossGroupMonthRankListModel rankModel = getCrossGroupRankListModelMap(groupId, month, true);
                    //加入容器
                    for (CrossUserRank crossUserRank : mapEntry.getValue().values()) {
                        rankModel.addUserRank(crossUserRank);
                    }
                    rankModel.setRankNum(rankNum);
                    rankModel.endSort();

                    //档次分母
                    int rankDenominator = Math.min(GameConfig.UNION_PLAYER_MONTH_ABILITY_RANK_NUM_MAX, rankNum);

                    getLogger().info("组id={},区服{},开始评定,总人数{},分母{}", groupId, JSONObject.toJSONString(serverGroup.getServerList()), rankModel.getNowRankNum(), rankDenominator);

                    for (CrossUserRank crossUserRank : rankModel.getRankList()) {
                        int rankGrade = ConfigMgr.getMonthAbilityRankToGrade(crossUserRank.getValue(), crossUserRank.getRank(), rankDenominator, minValueMap);
                        crossUserRank.setParam(String.valueOf(rankGrade));
                    }
                    rankModel.setUpdate(true);
                    //重新生成排行消息,把档次放进去
                    rankModel.initRankMsg();

                    //保存榜单大小
                    monthRankUpload.setRankNum(groupId, rankNum);
                    //更新评定时间
                    monthRankUpload.setRankTime(groupId, DateHelper.getCurrentSecond());

                    CrossUnionMgr.updateMemberMonthRank(groupId);
                }

                //评定结束移出内存
                groupUserDataCatch.remove(serverNum);
            }
        }

        synchronized (groupUploadOverMap) {
            for (CrossUnionServerGroup serverGroup : serverGroups) {
                int groupId = serverGroup.getGroupId();
                Map<Integer, Boolean> monthMap = groupUploadOverMap.get(groupId);
                if (null == monthMap) {
                    monthMap = new ConcurrentHashMap<>();
                    groupUploadOverMap.put(groupId, monthMap);
                }
                monthMap.put(month, isOver);
            }
        }

        if (isLoad && !isOver){
            //重载时如果没有全部上传完，清理上传标志重新拉数据
            return false;
        }

        return true;
    }

    public static CrossGroupMonthRankListModel getCrossGroupRankListModelMap(int groupId, int month, boolean newIfNull) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroup(groupId);
        if (null != serverGroup) {
            Map<Integer, CrossGroupMonthRankListModel> modelMap = groupRankListModelMap.get(groupId);
            if (null == modelMap) {
                if (newIfNull) {
                    synchronized (groupRankListModelMap) {
                        modelMap = groupRankListModelMap.get(groupId);
                        if (null == modelMap) {
                            modelMap = new ConcurrentHashMap<>();
                            groupRankListModelMap.put(groupId, modelMap);
                            CrossGroupMonthRankListModel model = new CrossGroupMonthRankListModel(groupId, month, eBigRankType.Normal.getValue(), eRankType.CorssHistoryMaxAbility.getValue());
                            modelMap.put(month, model);
                        }
                        return modelMap.get(month);
                    }
                }
            } else {
                CrossGroupMonthRankListModel model = modelMap.get(month);
                if (null == model && newIfNull) {
                    synchronized (groupRankListModelMap) {
                        model = modelMap.get(month);
                        if (null == model) {
                            model = new CrossGroupMonthRankListModel(groupId, month, eBigRankType.Normal.getValue(), eRankType.CorssHistoryMaxAbility.getValue());
                            modelMap.put(month, model);
                        }
                    }
                }
                return model;
            }
        }
        return null;
    }

    public static CrossGroupNumMonthRankListModel getCrossGroupNumRankListModelMap(int groupServerNum, int month, boolean newIfNull) {
        if (groupServerNum < GameConfig.UNION_PLAYER_OPEN_COND_AREA_NUM) {
            return null;
        }
        Map<Integer, CrossGroupNumMonthRankListModel> modelMap = groupNumRankListModelMap.get(groupServerNum);
        if (null == modelMap) {
            if (newIfNull) {
                synchronized (groupNumRankListModelMap) {
                    modelMap = groupNumRankListModelMap.get(groupServerNum);
                    if (null == modelMap) {
                        modelMap = new ConcurrentHashMap<>();
                        groupNumRankListModelMap.put(groupServerNum, modelMap);
                        CrossGroupNumMonthRankListModel model = new CrossGroupNumMonthRankListModel(groupServerNum, month, eBigRankType.Normal.getValue(), eRankType.CorssHistoryMaxAbility.getValue());
                        modelMap.put(month, model);
                    }
                    return modelMap.get(month);
                }
            }
        } else {
            CrossGroupNumMonthRankListModel model = modelMap.get(month);
            if (null == model && newIfNull) {
                synchronized (groupNumRankListModelMap) {
                    model = modelMap.get(month);
                    if (null == model) {
                        model = new CrossGroupNumMonthRankListModel(groupServerNum, month, eBigRankType.Normal.getValue(), eRankType.CorssHistoryMaxAbility.getValue());
                        modelMap.put(month, model);
                    }
                }
            }
            return model;
        }
        return null;
    }

    public static void receiveUserGroupRank(int groupId, List<CrossUserRank> crossUserRankList, long serverId, boolean isEnd) {
        int serverNum = CrossUnionServerGroupMgr.getGroupServer(groupId).size();
        Map<Integer, Map<Long, CrossUserRank>> groupRankMap = groupUserDataCatch.get(serverNum);
        if (null == groupRankMap) {
            synchronized (groupUserDataCatch) {
                groupRankMap = groupUserDataCatch.get(serverNum);
                if (null == groupRankMap) {
                    groupRankMap = new ConcurrentHashMap<>();
                    groupUserDataCatch.put(serverNum, groupRankMap);
                }
            }
        }
        Map<Long, CrossUserRank> rankMap = groupRankMap.get(groupId);
        if (null == rankMap) {
            synchronized (groupUserDataCatch) {
                rankMap = groupRankMap.get(groupId);
                if (null == rankMap) {
                    rankMap = new ConcurrentHashMap<>();
                    groupRankMap.put(groupId, rankMap);
                }
            }
        }
        for (CrossUserRank crossUserRank : crossUserRankList) {
            rankMap.put(crossUserRank.getUserId(), crossUserRank);
        }

        if (isEnd) {
            addUploadOverServer(serverId);
        }
    }

    public static void addUploadOverServer(long serverId) {
        int month = DateHelper.getMonth();
        synchronized (uploadOverServerIdsMap) {
            CrossMonthRankUploadOver data = uploadOverServerIdsMap.get(month);
            if (null == data) {
                data = new CrossMonthRankUploadOver();
                data.setMonth(month);
                data.setInsertOption();
                uploadOverServerIdsMap.put(month, data);
            }
            data.addServerId(serverId);
        }
    }

    public static boolean isUploadOver(long serverId) {
        int month = DateHelper.getMonth();
        synchronized (uploadOverServerIdsMap) {
            CrossMonthRankUploadOver data = uploadOverServerIdsMap.get(month);
            if (null == data) {
                return false;
            }
            return data.getServerIds().contains(serverId);
        }
    }

    private void addServerGroupReloadListener() {
        CrossUnionServerGroupMgr.addReloadListener(() -> {
            serverGroupReload();
            initBeforeChangeGroupServerList();
            logger.info("server group reload");
        });
    }

    public static void serverGroupReload() {
        //区服重载，排行榜重新分组
        int month = DateHelper.getMonth();
        Set<Integer> serverNumSet = new HashSet<>();
        List<Integer> removeGroupList = new ArrayList<>();
        List<CrossUnionServerGroup> groupList = CrossUnionServerGroupMgr.getServerGroupList(ConfigHelper.getInt("channelId"));
        for (CrossUnionServerGroup serverGroup : groupList) {
            if (serverGroup.getState() != 1) {
                continue;
            }
            int serverNum = serverGroup.getServerList().size();
            if (serverNum < GameConfig.UNION_PLAYER_OPEN_COND_AREA_NUM) {
                continue;
            }
            int groupId = serverGroup.getGroupId();
            Set<Long> serverIdSet = new HashSet<>(serverGroup.getServerList());
            //判断组内服务器有没有变化,清理标志，重新拉数据
            boolean isGroupChange = false;
            Set<Long> beforeGrouServerList = beforeChangeGroupServerList.getOrDefault(serverGroup.getGroupId(), new HashSet<>());
            if (beforeGrouServerList.size() != serverIdSet.size()) {
                isGroupChange = true;
            } else {
                for (Long serverId : serverIdSet) {
                    if (!beforeGrouServerList.contains(serverId)) {
                        isGroupChange = true;
                        break;
                    }
                }
            }

            if (isGroupChange) {
                serverNumSet.add(serverIdSet.size());
                if (beforeGrouServerList.size() >= GameConfig.UNION_PLAYER_OPEN_COND_AREA_NUM) {
                    serverNumSet.add(beforeGrouServerList.size());
                }
                if (!removeGroupList.contains(groupId)) {
                    removeGroupList.add(groupId);
                }

                CrossGroupMonthRankListModel rankListModel = getCrossGroupRankListModelMap(groupId, month, false);
                if (null != rankListModel) {
                    rankListModel.resetRankValue();
                }
            }
        }

        if(removeGroupList.size() > 0) {
            synchronized (groupUploadOverMap) {
                for(int groupId : removeGroupList) {
                    groupUploadOverMap.remove(groupId);
                }
            }
        }

        if (!serverNumSet.isEmpty()) {//把指定区服数的跨服分组上传完成标志清除
            removeUploadServerIdByServerNum(serverNumSet);
        }
    }

    /**
     * 把指定区服数的跨服分组上传完成标志清除,会重新拉数据
     *
     * @param serverNumSet
     */
    private static void removeUploadServerIdByServerNum(Set<Integer> serverNumSet) {
        CrossMonthRankUploadOver rankUploadOver = uploadOverServerIdsMap.get(DateHelper.getMonth());
        List<CrossUnionServerGroup> serverGroupList = CrossUnionServerGroupMgr.getServerListMapByServerNum(serverNumSet, GameConfig.UNION_PLAYER_OPEN_COND_AREA_NUM);
        for (CrossUnionServerGroup serverGroup : serverGroupList) {
            if (null != rankUploadOver) {
                synchronized (rankUploadOver) {
                    //把该分组的上传完成标志清除
                    for (Long serverId : serverGroup.getServerList()) {
                        rankUploadOver.getServerIds().remove(serverId);
                    }
                    rankUploadOver.setUpdateOption();
                }
            }
        }

        for (Integer serverNum : serverNumSet) {
            CrossGroupNumMonthRankListModel rankListModel = getCrossGroupNumRankListModelMap(serverNum, DateHelper.getMonth(), false);
            if (null != rankListModel) {
                rankListModel.resetRankValue();
            }
        }
    }

    public static Set<Long> getGroupServer(int groupId) {
        Set<Long> serverSet = new HashSet<>();
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroup(groupId);
        if (serverGroup != null && serverGroup.getState() == 1) {
            serverSet.addAll(serverGroup.getServerList());
        }
        return serverSet;
    }

    public static int getGroupId(long serverId) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if (serverGroup != null && serverGroup.getState() == 1) {
            return serverGroup.getGroupId();
        }
        return (int) serverId;
    }

    public static int getUserRank(long serverId, long userId) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if (null == serverGroup) {
            return 0;
        }
        CrossGroupMonthRankListModel rankListModel = getCrossGroupRankListModelMap(serverGroup.getGroupId(), DateHelper.getMonth(), false);
        if (null == rankListModel) {
            return 0;
        }

        CrossUserRank crossUserRank = rankListModel.getMyCrossUserRank(userId, serverId);
        if (null == crossUserRank) {
            return -1;
        }

        return crossUserRank.getRank();
    }

    public static CrossUserRank getCrossUserRank(long serverId, long userId) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if (null == serverGroup) {
            return null;
        }
        CrossGroupMonthRankListModel rankListModel = getCrossGroupRankListModelMap(serverGroup.getGroupId(), DateHelper.getMonth(), false);
        if (null == rankListModel) {
            return null;
        }

        CrossUserRank crossUserRank = rankListModel.getMyCrossUserRank(userId, serverId);

        return crossUserRank;
    }

    public static int getUserRankGradeByAbility(long serverId, BigInteger ability) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if (null == serverGroup) {
            return 0;
        }
        CrossGroupMonthRankListModel rankListModel = getCrossGroupRankListModelMap(serverGroup.getGroupId(), DateHelper.getMonth(), false);
        if (null == rankListModel) {
            return 0;
        }

        CrossUserRank crossUserRank = rankListModel.getCrossUserRankByValue(ability);
        if (null == crossUserRank){
            return ConfigMgr.getLastMonthAbilityRankGrade();
        }
        int grade = Integer.valueOf(crossUserRank.getParam());
        if (0 == grade){
            grade = ConfigMgr.getLastMonthAbilityRankGrade();
        }
        return grade;
    }

    /**
     * 取榜单长度
     *
     * @param serverId
     * @return
     */
    public static int getMonthAbilityRankMax(long serverId) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if (null == serverGroup) {
            return 0;
        }
        return getMonthAbilityRankMax(serverGroup.getGroupId());
    }

    public static int getMonthAbilityRankMax(int groupId) {
        CrossGroupMonthRankListModel rankListModel = getCrossGroupRankListModelMap(groupId, DateHelper.getMonth(), false);
        if (null == rankListModel) {
            return 0;
        }

        return rankListModel.getNowRankNum();
    }

    //过期数据移除
    public static void removeOutTimeData() {
        int nowMonth = DateHelper.getMonth();
        for (int groupId : groupRankListModelMap.keySet()) {
            Map<Integer, CrossGroupMonthRankListModel> monthMap = groupRankListModelMap.get(groupId);
            for (int month : monthMap.keySet()) {
                if (month != nowMonth) {
                    synchronized (groupRankListModelMap) {
                        monthMap.remove(month);
                    }
                }
            }
        }

        for (Integer month : uploadOverServerIdsMap.keySet()) {
            if (month != nowMonth) {
                synchronized (uploadOverServerIdsMap) {
                    uploadOverServerIdsMap.remove(month);
                }
            }
        }
        for (Integer groupId : groupUploadOverMap.keySet()) {
            Map<Integer, Boolean> monthMap = groupUploadOverMap.get(groupId);
            for (int month : monthMap.keySet()) {
                if (month != nowMonth) {
                    synchronized (groupUploadOverMap) {
                        monthMap.remove(month);
                    }
                }
            }
        }
    }

    public static boolean isServerGroupOver(long serverId) {
        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if (null == serverGroup || serverGroup.getState() == 0) {
            return true;
        }
        Map<Integer, Boolean> dataMap = groupUploadOverMap.get(serverGroup.getGroupId());
        if(null != dataMap){
            return dataMap.getOrDefault(DateHelper.getMonth(), false);
        }
        return false;
    }

    private static void initBeforeChangeGroupServerList() {
        Map<Integer, CrossUnionServerGroup> groupMap = CrossUnionServerGroupMgr.getServerGroupMap(ConfigHelper.getInt("channelId"));
        if (null != groupMap) {
            for (CrossUnionServerGroup crossUnionServerGroup : groupMap.values()) {
                beforeChangeGroupServerList.put(crossUnionServerGroup.getGroupId(), new ConcurrentHashSet<>(crossUnionServerGroup.getServerList()));
            }
        }
    }

    /**
     * 清除标志后,会重新拉取排行数据。修复用
     */
    public static void clearUploadMark(){
        uploadOverServerIdsMap.clear();
        groupUploadOverMap.clear();
    }


}
